<script lang="ts" setup>
import { useRouter } from "vue-router";
import { useUserStore } from "@/store/modules/user";
import { LockClosedOutline, PersonOutline } from "@vicons/ionicons5";
import { PageEnum } from "@/enums/pageEnum";
import WebBaseEnum from "@/enums/webBaseEnum";
import { useI18n } from "vue-i18n";
const { t } = useI18n();

const formRef = ref();
const loading = ref(false);
const message = useMessage();

const form = reactive({
  username: "administrator",
  password: "123456",
});
const rules = {
  username: {
    required: true,
    message: `${t("login.usernameRule")}`,
    trigger: "blur",
  },
  password: {
    required: true,
    message: `${t("login.passwordRule")}`,
    trigger: "blur",
  },
};

const userStore = useUserStore();
const router = useRouter();
const handleSubmit = (e) => {
  e.preventDefault();
  formRef.value.validate(async (errors: any) => {
    loading.value = true;
    if (!errors) {
      try {
        await userStore.login(toRaw(form));
        console.log(
          "router.currentRoute.value.query?.redirect",
          router.currentRoute.value.query?.redirect,
          router.currentRoute.value.name,
        );

        const toPath = decodeURIComponent(
          (router.currentRoute.value.query?.redirect || "/") as string,
        );
        message.destroyAll();
        if (router.currentRoute.value.name === PageEnum.BASE_LOGIN_NAME) {
          await router.push("/");
        } else {
          await router.push(toPath);
        }
      } finally {
        loading.value = false;
      }
    }
  });
};

function onCodeLogin() {
  message.warning("暂时没有接入短信登录方式");
}
</script>

<template>
  <div class="view-account home">
    <div class="view-account-header"></div>
    <div class="view-account-container">
      <div class="view-account-top">
        <div class="view-account-top-logo">
          <!-- <img :src="WebBaseEnum.loginImage" alt="" /> -->
        </div>
        <div class="text-2xl">{{ WebBaseEnum.title }}</div>
      </div>
      <div class="view-account-form _themeCard">
        <div class="text-center text-2xl mb-8">
          {{ WebBaseEnum.loginDescription }}
        </div>
        <n-form
          ref="formRef"
          :model="form"
          :rules="rules"
          label-placement="left"
          size="large"
        >
          <n-form-item class="login-animation1" path="username">
            <n-input
              v-model:value="form.username"
              :placeholder="t('login.namePlaceholder')"
            >
              <template #prefix>
                <n-icon color="#808695" size="18">
                  <PersonOutline />
                </n-icon>
              </template>
            </n-input>
          </n-form-item>
          <n-form-item class="login-animation2" path="password">
            <n-input
              v-model:value="form.password"
              :placeholder="t('login.passwordPlaceholder')"
              showPasswordOn="click"
              type="password"
            >
              <template #prefix>
                <n-icon color="#808695" size="18">
                  <LockClosedOutline />
                </n-icon>
              </template>
            </n-input>
          </n-form-item>
          <n-form-item class="login-animation3 mt-2">
            <n-button
              :loading="loading"
              block
              size="large"
              type="primary"
              @click="handleSubmit"
            >
              {{ t("login.loginBtn") }}
            </n-button>
          </n-form-item>
          <div class="login-animation4 mb-3 flex justify-end">
            <div class="mr-3">
              <n-button text type="info" @click="onCodeLogin">{{
                t("login.codeLogin")
              }}</n-button>
            </div>
            <div>
              <n-button
                text
                type="info"
                @click="router.push(PageEnum.BASE_REGISTER)"
              >
                {{ t("login.register") }}
              </n-button>
            </div>
          </div>
        </n-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.view-account {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: auto;

  &-container {
    flex: 1;
    padding: 32px 12px;
    max-width: 400px;
    min-width: 400px;
    margin: 0 auto;

    .view-account-form {
      border-radius: 15px;
      padding: 30px 45px 40px 45px;
    }
  }

  &-top {
    padding: 32px 0 25px 0;
    text-align: center;

    &-desc {
      font-size: 14px;
      color: #e3e3e428;
    }
  }

  &-other {
    width: 100%;
  }

  .default-color {
    color: #515a6e;

    .ant-checkbox-wrapper {
      color: #515a6e;
    }
  }
}

@media (min-width: 768px) {
  .view-account {
    background-image: url("../../assets/images/login.svg");
    background-repeat: no-repeat;
    background-position: 50%;
    background-size: 100%;
  }

  .page-account-container {
    padding: 32px 0 24px 0;
  }
}

@keyframes anim-num {
  0% {
    transform: translateY(60px);
    opacity: 0;
  }
  100% {
    transform: translateY(0);
    opacity: 1;
  }
}
.login-animation1 {
  opacity: 0;
  animation-name: anim-num;
  animation-duration: 0.5s;
  animation-fill-mode: forwards;
  animation-delay: 0.1s;
}
.login-animation2 {
  opacity: 0;
  animation-name: anim-num;
  animation-duration: 0.5s;
  animation-fill-mode: forwards;
  animation-delay: 0.2s;
}
.login-animation3 {
  opacity: 0;
  animation-name: anim-num;
  animation-duration: 0.5s;
  animation-fill-mode: forwards;
  animation-delay: 0.3s;
}
.login-animation4 {
  opacity: 0;
  animation-name: anim-num;
  animation-duration: 0.5s;
  animation-fill-mode: forwards;
  animation-delay: 0.4s;
}
.login-animation5 {
  opacity: 0;
  animation-name: anim-num;
  animation-duration: 0.5s;
  animation-fill-mode: forwards;
  animation-delay: 0.5s;
}
</style>
